var already_quoted = false;

$(function() {
    $('.topic_reply_btn').click(function() {
        clear_quote();
        $('#repost').focus();
    });

    $('.topic_edit_btn').click(function() {
        //TODO:
        location.href = (window.EB_DEV ? "/ourplayground" : "") + "/construct"
    });
    
    $(document)
        .on('mouseenter', '.repost_single', function() {
            $(this).find('.report_btn').show();
        })
        .on('mouseleave', '.repost_single', function() {
            $(this).find('.report_btn').hide();
        })
        .on('click', '.comment_btn', function() {
            var $ancestor = $('.popover-inner').has(this);
                quote_title = $ancestor.find('.popover-title').html();
                repost_content = $ancestor.find('.repost_content').html();
                repost_id = $('.repost_single').has(this).attr('id');
                $quote_title = $('#repost_quote .quote_title');
                
            $quote_title.html(quote_title).find('.delete_btn').remove();
            $('#repost_quote .quote_content div').html(repost_content);
            $('#repost_quote').show();
            
            var oldAction = $('#repost_form').attr('action');
            if (already_quoted) {
                var newAction = oldAction.substring(0, oldAction.lastIndexOf('/'));
                $('#repost_form').attr('action', newAction + '/' + repost_id);
            } else {
                $('#repost_form').attr('action', oldAction + '/' + repost_id);
                already_quoted = true;            
            }
            
            if ($quote_title.find('.repost_private_mark').length > 0) { 
                $('#repost_private').attr('checked', 'checked').attr('disabled', 'disabled');
            } else {
                $('#repost_private').removeAttr('disabled');
            }
            
            $('#repost').focus();
        })
        .on('click', '.report_btn a', function() {
            alert('感谢您的举报，我们将尽快做出处理！');
        })
        .on('click', '.delete_btn', function() {
            var post_id = $('#post_id').attr('value');
                repost_id = $('.repost_single').has(this).attr('id');
            delete_repost(post_id, repost_id);
        });;
    
    $('#repost_form').submit(function() {
        // unset disable to submit in form
        $('#repost_private').removeAttr('disabled');
        // use $(this.repost_content).val()
        // to do keyword filter
    });
    
    $('#repost_submit').click(function() {
        is_login(function() {
            $('#repost_form').submit();
        });
        return false;
    });
    
    $('#clear_quote').click(clear_quote);
    
    $('#repost_clear').click(function() {
        $('#repost').val('').focus();
    });
    
    $('#repost').autosize();
    
    $('.latest_btn').click(function() {
        if ($('.pagination li').length == 1) {
            var latest = _last_repost_id();
            location.hash = latest;
            return false;
        }
    });
    // 简单识别出帖子内的真相而不是表情
    $('.post_content img[title]').addClass('post_photo');
    $('.post_photo').click(function() {
        var post_id = $('#post_id').attr('value');    
        var post_title = $('.post_title').attr('data-content');
        post_photo_carousel(post_id, post_title);
    });

    load_repost();
});

function clear_quote() {
    if (already_quoted) {
        var oldAction = $('#repost_form').attr('action');
        var newAction = oldAction.substring(0, oldAction.lastIndexOf('/'));
        $('#repost_form').attr('action', newAction);
        $('#repost_quote').hide();
        already_quoted = false;
        
        $('#repost_private').removeAttr('disabled');
    }
}

function load_repost() {
    var post_id = $('#post_id').attr('value');
    var pn = $('#repost_list').attr('value');
    $('#repost_list').showLoading();
    $.post('repost/any', {
        'post_id': post_id,
        'pn': pn
    }, function(data) {
        $('#repost_list').hideLoading().empty().append(data.html);
        if (pn != 1) {
            $body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body');//这行是 Opera 的补丁, 少了它 Opera 是直接用跳的而且画面闪烁 by willin
            $body.animate({scrollTop: $('#repost_list').offset().top}, 10, function() {
                _jump_to_hash();
            });
        }

        $('.repost_single[id]').each(function() {
            $(this).fadeIn('slow', function() {
                if ('#' + this.id == location.hash) {
                    _jump_to_hash();
                }
            });
        });
    }, 'json');
}

function delete_repost(post_id, repost_id) {
    if (confirm('您确认要删除该回复吗？\n回复过少会影响您帖子的热门程度和排序哦 亲~~')) {
        $.post('repost/delete', {
            post_id: post_id,
            repost_id: repost_id
        }, function(data) {
            if (data != 'error') {
                location.reload();
            }
        });
    }
}

function _jump_to_hash() {
    var hash = location.hash;
    if (hash != '') {
        location.hash = '';
        location.hash = hash;
    }
}

function _last_repost_id() {
    var repost_id = $('#repost_list .repost_single').last().attr('id');
    if (repost_id == undefined) {
        return 0;
    } else {
        return repost_id;
    }
}